/**
 * cookie 操作
 * @author ck.liu
 * @date 2017/9/22 20:52
 */
export default {

	/* 设置cookie */
    set(key, value, domain, sec){
        if(key == null) { console.log(`parameter 'key' can not be empty`); return; };
        if(value == null) { console.log(`parameter 'value' can not be empty`); return; }

        let cookie = `${key}=${escape(value)};`;
        domain && (cookie += `domain=${domain};`);

        /* 默认30天 */
        sec = sec || 30 * 24 * 60 * 60;     
        let expDate = new Date();
        expDate.setDate(expDate.getTime() + sec * 1000);
        cookie += `expires=${expDate.toGMTString()};`;
        document.cookie = cookie;
    },

    /* 获取cookie */
    get(key){
        if(key == null) { console.log(`parameter 'key' can not be empty`); return; };
        console.log(document.cookie);
        let cookieList = document.cookie.split(";");
        console.log(cookieList);
        for(let i = 0; i < cookieList.length; i++){
            let cookie = cookieList[i].trim().split("=");
            if(cookie.length > 1){
                let val = unescape(cookie[1]);
                if(cookie[0] === key && val !== '#'){
                    return val;
                }
            }
        }
        return null;
    },

    /* 删除cookie，将过期时间设置为一个过去的时间，并将内容设置为'#' */
    remove(key){
        if(key == null) { console.log(`parameter 'key' can not be empty`); return; };

        let exp = new Date();
        exp.setDate(exp.getTime() - 1);
        document.cookie = `key=${escape('#')};expires=${exp.toGMTString()}`;
    }
    
}